#include <reg52.h>

sbit ADDR0 = P1 ^ 0; // 通过跳线器 把p1^0引脚和38译码器的ADDR0口连接起来  数据位
sbit ADDR1 = P1 ^ 1; // 通过跳线器 把p1^1引脚和38译码器的ADDR1口连接起来  数据位
sbit ADDR2 = P1 ^ 2; // 通过跳线器 把p1^2引脚和38译码器的ADDR2口连接起来  数据位
sbit ADDR3 = P1 ^ 3; // 通过跳线器 把p1^3引脚和38译码器的ADDR3口连接起来   使能ENLED=0，ADDR3=1 固定
sbit ENLED = P1 ^ 4; // 使能ENLED=0，ADDR3=1 固定

// 数码管真值表
unsigned char code LedChar[] = {
    0xC0, 0xF9, , 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0xBE} void void
main()
{
    unsigned char i = 0; // 动态扫描的索引
    unsigned char cnt = 0;
    unsigned long sec = 0;
    //*********************38译码器 固定配置***********************************************
    ENLED = 0; // 使能开关 关闭
    ADDR3 = 1;
    // ADDR2 = 0;
    // ADDR1 = 0;
    // ADDR0 = 0; // 38译码器 真值   0111 1111
    //************************************************************************************
    //*********************定时器***********************************************
    TMOD = 0x01;
    TH0 = 0xFC;
    TL0 = 0x67;
    TR0 = 1;

    while (1)
    {
        if (TF0 == 1)
        {
            TF0 = 0;
            TH0 = 0xFC;
            TL0 = 0x67;
            cnt++;
            if (cnt >= 1000)
            {
                cnt = 0;
                //********************************************************************
                sec++;
                LedBuff[0] = LedChar[sec % 10];
                LedBuff[1] = LedChar[sec / 10 % 10];
                LedBuff[2] = LedChar[sec / 100 % 10];
                LedBuff[3] = LedChar[sec / 1000 % 10];
                LedBuff[4] = LedChar[sec / 10000 % 10];
                LedBuff[5] = LedChar[sec / 10000 % 10];
            }
        }
    }
}

// 中断函数
void InterruptTimer0() interrupt 1
{
    TH0 = 0xFC;
    TL0 = 0x67;
    cnt++;
    if (cnt >= 1000)
    {
        cnt = 0;
        flag1s = 1;
    }
    P0 = 0xFF; // 显示消隐
    switch (i)
    {
    case 0:
        ADDR2 = 0;
        ADDR1 = 0;
        ADDR0 = 0;
        i++;
        P0 = LedBuff[0];
        break;
    case 1:
        ADDR2 = 0;
        ADDR1 = 0;
        ADDR0 = 1;
        i++;
        P0 = LedBuff[1];
        break;
    case 2:
        ADDR2 = 0;
        ADDR1 = 1;
        ADDR0 = 0;
        i++;
        P0 = LedBuff[2];
        break;
    case 3:
        ADDR2 = 0;
        ADDR1 = 1;
        ADDR0 = 1;
        i++;
        P0 = LedBuff[3];
        break;
    case 4:
        ADDR2 = 1;
        ADDR1 = 0;
        ADDR0 = 0;
        i++;
        P0 = LedBuff[4];
        break;
    case 5:
        ADDR2 = 1;
        ADDR1 = 0;
        ADDR0 = 1;
        i = 0;
        P0 = LedBuff[5];
        break;
    default:
        break;
    }
}